import math
from openpyxl import load_workbook
import os


def cover_width_negative_func(depth, slope):
    return (
        depth * math.sin(math.pi / 3) / math.sin(math.pi / 6 - slope) * math.cos(slope)
    )


def cover_width_positive_func(depth, slope):
    return (
        depth * math.sin(math.pi / 3) / math.sin(math.pi / 6 + slope) * math.cos(slope)
    )


def cover_width_func(depth, slope):
    return cover_width_negative_func(depth, slope) + cover_width_positive_func(
        depth, slope
    )


def one():
    a = math.radians(1.5)
    center_depth = 70
    interval = 200
    relative_distance_list = [-800, -600, -400, -200, 0, 200, 400, 600, 800]
    header = [
        "测线距中心点处的距离/m",
        "海水深度/m",
        "覆盖宽度/m",
        "与前一条测线的重叠率/%",
    ]
    depth_list = [
        center_depth - relative_distance * math.tan(a)
        for relative_distance in relative_distance_list
    ]
    cover_width_positive_list = [
        cover_width_positive_func(depth, a) for depth in depth_list
    ]
    cover_width_negative_list = [
        cover_width_negative_func(depth, a) for depth in depth_list
    ]
    cover_width_list = [
        x + y for x, y in zip(cover_width_negative_list, cover_width_positive_list)
    ]
    cover_rate_list = ["-"] + [
        (
            (cover_width_negative_list[index] + cover_width_positive_list[index - 1])
            - interval
        )
        / cover_width_list[index]
        * 100
        for index in range(1, len(cover_width_list))
    ]
    wb = load_workbook("result1.xlsx")
    ws = wb.active
    for i in range(9):
        ws.cell(row=2, column=2 + i, value=depth_list[i])
        ws.cell(row=3, column=2 + i, value=cover_width_list[i])
        ws.cell(row=4, column=2 + i, value=cover_rate_list[i])
    os.makedirs("output", exist_ok=True)
    wb.save("output/result1.xlsx")


if __name__ == "__main__":
    one()
